const app = require("../app");
const {
    REQUIRED,
    EXISTS,
    NOEXISTS,
    INCORRENT,
    UNAUTHORIZATION,
    NOPOWER,
    BIND,
	UPVOTE
} = require("../config/error.js");

// 监听error事件
app.on("error", (error, ctx) => {
    let code = 0;
    let message = "";

    switch (error) {
        case REQUIRED:
            code = -1001;
            message = "用户名或密码不能为空~";
            break;
        case EXISTS:
            code = -1002;
            message = "该用户名已经存在~";
            break;
        case NOEXISTS:
            code = -1003;
            message = "该用户不存在~";
            break;

        case INCORRENT:
            code = -1004;
            message = "密码错误~";
            break;

        case UNAUTHORIZATION:
            code = -1005;
            message = "token已经过期";
            break;

        case NOPOWER:
            code = -1006;
            message = "没有操作权限~";
            break;

        case BIND:
            code = -1007;
            message = "已经绑定过该信息~";
            break;
			
		case UPVOTE:
			code = -1007;
			message = "已经点过赞了~";
			break;
    }
    // 抛出错误
    ctx.body = { code, message };
});
